package lbframework.bean.vencimento;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.JPopupMenu;
import javax.swing.SwingConstants;
import lbframework.classe.Util;
import lbframework.classe.LBException;
import lbframework.bean.panel.LBPanel;
import lbframework.bean.table.LBColumnTable;
import lbframework.remote.ItemComboVO;

public class LBVencimento extends LBPanel {

    private VencimentoVO oVencimento = null;
    public Vector<VencimentoVO> vVencimento = null;
    private JPopupMenu popUp = new JPopupMenu();
    private boolean enabled = true;

    public LBVencimento() {
        initComponents();
        vVencimento = new Vector();

        cboVencimento.setEnabled(true);

    }

    public void adicionarVencimento() throws Exception {
        popUp.add(pnlPopUp);
        int x = cboVencimento.getX();
        int y = cboVencimento.getY() + cboVencimento.getHeight();

        popUp.setSize(pnlPopUp.getWidth(), pnlPopUp.getHeight());
        popUp.show(this, x, y);
        txtVencimento.requestFocus();

    }

    public void adicionarVencimentoCombo() throws Exception {
        cboVencimento.removeAllItems();

        if (vVencimento.size() != 0) {
            for (VencimentoVO oDataAdd : vVencimento) {
                cboVencimento.addItem(new ItemComboVO(oDataAdd.id, oDataAdd.vencimento));
            }
        }
        cboVencimento.setId(-1);
    }

    private void adicionarVencimentos() throws Exception {
        if (!txtVencimento.getClipText().equals("")) {
            oVencimento = new VencimentoVO();

            oVencimento.idVencimento = vVencimento.size();
            oVencimento.vencimento = txtVencimento.getText();

            vVencimento.add(oVencimento);

            exibirVencimentos();
            adicionarVencimentoCombo();

            txtVencimento.setText("");
            txtVencimento.requestFocus();
        }
    }

    public void addItem(Vector<VencimentoVO> vVencimentoAdd) throws Exception {
        int i = 0;

        vVencimento.addAll(vVencimentoAdd);

        for (VencimentoVO oAddVencimento : vVencimentoAdd) {
            cboVencimento.addItem(new ItemComboVO(i, String.valueOf(oAddVencimento.vencimento)));
            cboVencimento.setId(-1);
            i++;
        }
    }

    private void exibirVencimentos() throws Exception {
        LBColumnTable[] colunas = new LBColumnTable[]{
            new LBColumnTable("Vencimento", 100, true, SwingConstants.LEFT, false, null),};

        Object[][] dados = new Object[vVencimento.size()][colunas.length];

        int i = 0;

        for (VencimentoVO oDatas : vVencimento) {
            dados[i][0] = oDatas.vencimento;
            i++;
        }
        tblData.setModel(dados, colunas);
    }

    public void clear() throws Exception {
        vVencimento.clear();
        cboVencimento.removeAllItems();
    }

    public int getItemCount() throws Exception {
        return cboVencimento.getItemCount();
    }

    public int getId() throws Exception {
        return cboVencimento.getId();
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    @Override
    public void setEnabled(boolean value) {
        this.enabled = value;
        btnData.setEnabled(value);
    }

    @Override
    public void setBackground(Color bg) {
        if (cboVencimento != null) {
            cboVencimento.setBackground(bg);
        }

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlPopUp = new lbframework.bean.panel.LBPanel();
        LBLabel1 = new lbframework.bean.label.LBLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblData = new lbframework.bean.table.LBTable();
        btnEnvia = new lbframework.bean.button.LBButton();
        txtVencimento = new lbframework.bean.calendar.LBCalendar();
        btnData = new lbframework.bean.button.LBButton();
        cboVencimento = new lbframework.bean.comboBox.LBComboBox();

        pnlPopUp.setBorder(null);

        LBLabel1.setText("Vencimento");

        tblData.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tblDataKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tblData);

        btnEnvia.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/enviar.png"))); // NOI18N
        btnEnvia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEnviaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlPopUpLayout = new javax.swing.GroupLayout(pnlPopUp);
        pnlPopUp.setLayout(pnlPopUpLayout);
        pnlPopUpLayout.setHorizontalGroup(
            pnlPopUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPopUpLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPopUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnlPopUpLayout.createSequentialGroup()
                        .addComponent(txtVencimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEnvia, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(LBLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        pnlPopUpLayout.setVerticalGroup(
            pnlPopUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPopUpLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(LBLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addGroup(pnlPopUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtVencimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEnvia, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                .addContainerGap())
        );

        setBorder(null);
        setMinimumSize(new java.awt.Dimension(109, 20));
        setPreferredSize(new java.awt.Dimension(109, 20));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnData.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/bean/vencimento/vencimento.png"))); // NOI18N
        btnData.setToolTipText("Adicionar Vencimento (F6)");
        btnData.setFocusable(false);
        btnData.setPreferredSize(new java.awt.Dimension(20, 20));
        btnData.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDataActionPerformed(evt);
            }
        });
        btnData.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                btnDataFocusLost(evt);
            }
        });
        add(btnData, new org.netbeans.lib.awtextra.AbsoluteConstraints(85, 0, 24, 20));

        cboVencimento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cboVencimentoKeyPressed(evt);
            }
        });
        add(cboVencimento, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 80, 20));
    }// </editor-fold>//GEN-END:initComponents

    private void btnDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDataActionPerformed
        try {
            if (enabled) {
                adicionarVencimento();
            }
        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
    }//GEN-LAST:event_btnDataActionPerformed

    private void btnDataFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_btnDataFocusLost
        try {
            adicionarVencimentoCombo();
        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
    }//GEN-LAST:event_btnDataFocusLost

    private void cboVencimentoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cboVencimentoKeyPressed
        try {
            if (evt.getKeyCode() == KeyEvent.VK_F6) {
                if (enabled) {
                    adicionarVencimento();
                }
            }
        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
    }//GEN-LAST:event_cboVencimentoKeyPressed

    private void btnEnviaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEnviaActionPerformed
        try {
            adicionarVencimentos();
        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
}//GEN-LAST:event_btnEnviaActionPerformed

    private void tblDataKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblDataKeyPressed
        try {
            if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
                vVencimento.remove(tblData.getLinhaSelecionada());
                exibirVencimentos();
            } else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                this.transferFocus();
                popUp.setVisible(false);
            }
        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
}//GEN-LAST:event_tblDataKeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private lbframework.bean.button.LBButton btnData;
    private lbframework.bean.button.LBButton btnEnvia;
    private lbframework.bean.comboBox.LBComboBox cboVencimento;
    private javax.swing.JScrollPane jScrollPane1;
    private lbframework.bean.panel.LBPanel pnlPopUp;
    private lbframework.bean.table.LBTable tblData;
    private lbframework.bean.calendar.LBCalendar txtVencimento;
    private lbframework.bean.label.LBLabel LBLabel1;
    // End of variables declaration//GEN-END:variables
}
